package com.briup.srms.exam.web.controller;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

import com.briup.srms.sms.bean.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.briup.srms.exam.bean.Exam;
import com.briup.srms.exam.bean.extend.ExamExtend;
import com.briup.srms.exam.bean.extend.StudentExamExtend;
import com.briup.srms.exam.service.IExamService;
import com.briup.srms.exam.vm.ExamVM;
import com.jacky.base.utils.ExcelUtils;
import com.jacky.base.utils.FileUtils;
import com.jacky.base.utils.Message;
import com.jacky.base.utils.MessageUtil;
import com.jacky.base.utils.PageVM;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @program: 杰普基础管理系统
 * @description: 考试的控制器类
 * @author: charles
 * @create: 2021-1-14 11:09
 **/
@Api(description="考试相关接口")
@Validated
@RestController
@RequestMapping("/exam/exam")
public class ExamController {

	@Autowired
	private IExamService examService;

	@ApiOperation(value = "根据学生id查询考试信息")
	@GetMapping("queryByStudentId")
	@ApiImplicitParams({ @ApiImplicitParam(name = "studentId", value = "学生编号", required = true, paramType = "query")})
	public Message queryByStudentId(@NotNull Long studentId) {
		List<Exam> exams = examService.queryByStudentId(studentId);
		return MessageUtil.success(exams);
	}

	@ApiOperation(value = "根据学生id、考试id查询学生考试信息",notes="用于学生答卷")
	@GetMapping("queryStudentExam")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "studentId", value = "学生编号", required = true, paramType = "query"),
		@ApiImplicitParam(name = "examId", value = "考试编号", required = true, paramType = "query")
	})
	public Message queryStudentExam(@NotNull Long studentId,@NotNull Long examId) {
		StudentExamExtend se = examService.queryStudentExam(studentId,examId);
		return MessageUtil.success(se);
	}

	@ApiOperation(value = "根据id查询考试详情信息")
	@GetMapping("queryById")
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "考试编号", required = true, paramType = "query")})
	public Message queryById(@NotNull Long id) {
		ExamExtend exam = examService.queryById(id);
		return MessageUtil.success(exam);
	}


	@ApiOperation(value = "分页多条件获取考试信息信息")
	@GetMapping("pageQuery")
	@ApiImplicitParams({ @ApiImplicitParam(name = "page", value = "当前页", required = true, paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, paramType = "query"), })
	public Message pageQuery(int page, int pageSize) {
		PageVM<Exam> pageVM = examService.pageQuery(page, pageSize);
		return MessageUtil.success(pageVM);
	}

	@ApiOperation(value = "通过id删除考试信息")
	@ApiImplicitParams(@ApiImplicitParam(name = "id", value = "主键", paramType = "query", required = true))
	@GetMapping("deleteById")
	public Message deleteById(long id) {
		examService.deleteById(id);
		return MessageUtil.success("删除成功");
	}

	@ApiOperation(value = "保存或更新考试信息")
	@PostMapping("saveOrUpdate")
	public Message saveOrUpdate(ExamVM exam) {
		examService.saveOrUpdate(exam,null);
		return MessageUtil.success("更新成功");
	}


	@ApiOperation(value = "下载参与招聘考试人员的模板", produces = "application/octet-stream")
	@GetMapping("downloadRecruitExamStudentTemplate")
	public void downloadRecruitExamStudentTemplate(HttpServletResponse response) throws Exception {
		// 2. 将excel导出
		// 2.1 excel头部数据
		String[] headList = new String[] {"身份证号", "学生姓名"};
		// 2.2 属性
		String[] fieldList = new String[] {"idCard" ,"realname" };
		// 2.3 数据列，需要转化
		List<Map<String, Object>> dataList = new ArrayList<>();
		// 2.4 导出文件名
		String excelName = "student_recruit_template";
		// 3. 调用工具方法导出Excel
		ExcelUtils.createExcel(response, excelName, headList, fieldList, dataList);
	}

	@ApiOperation(value = "保存招聘考试信息")
	@PostMapping("saveRecruitExam")
	public Message saveRecruitExam(@RequestParam("file") MultipartFile file, HttpServletRequest req,ExamVM exam) throws Exception {
		File toFile = null;
		if (file.getSize() <= 0) {
			file = null;
		} else {
			InputStream ins = null;
			ins = file.getInputStream();
			toFile = new File(file.getOriginalFilename());
			FileUtils.inputStreamToFile(ins, toFile);
			ins.close();
		}
		// 解析数据
		List<List<Object>> list = ExcelUtils.readExcel(toFile);
		List<Student> students = new ArrayList<>();
		for (List<Object> props : list) {
			Student student = new Student();
			// 设置渠道
			// 设置基本信息
			for (int i=0;i<props.size();i++) {
				Object prop = props.get(i);
				System.out.println(prop);
				switch (i) {
				case 0:
					student.setIdCard((String)prop);
					break;
				case 1:
					student.setRealname((String)prop);
					break;
				default:
					break;
				}
			}
			students.add(student);
		}
		examService.saveOrUpdate(exam,students);
		// 调用业务逻辑层代码完成批量保存操作
		return MessageUtil.success("创建成功");
	}

}
